


# 新数据库与R包尝试分析药敏-------
a <- oncoPredict_calcPhenotype(valid_exp = train_data$tumor_exprs, database = "GDSC2",
    drug_list = NULL, train_exp = NULL, train_ptype = NULL, od = file.path(od))

write_tsv(x = a %>%
    rownames_to_column("sample"), file.path(od, "out/6.GDSC2_drug/IC50_res.tsv"))
    
IC50 <- data.table::fread(file.path(out_home, "/out/8.drug/calcPhenotype_Output/DrugPredictions.csv")) %>%
    as.data.frame() %>%
    mutate_if(is.numeric, log2) %>%
    rename(sample = 1) %>%
    column_to_rownames("sample")

library(psych)
tcga_df <- major$training[[3]]

samples <- intersect(rownames(IC50), rownames(major$training[[3]] %>%
    select(sample, riskscore) %>%
    column_to_rownames("sample")))

cor_res <- corr.test(IC50[samples, ], major$training[[3]] %>%
    select(sample, riskscore) %>%
    column_to_rownames("sample") %>%
    .[samples, ], method = "spearman", adjust = "BH")

cor_res_df <- inner_join(cor_res$r %>%
    as.data.frame() %>%
    rownames_to_column("drug") %>%
    rename(rho = 2), cor_res$p.adj %>%
    as.data.frame() %>%
    rownames_to_column("drug") %>%
    rename(adjp = 2))

# 正相关
drug_name <- cor_res_df %>%
    filter(rho > 0) %>%
    arrange(adjp) %>%
    head() %>%
    pull(1)
a <- Drug_IC50(DrugInfor = IC50 %>%
    rownames_to_column("sample"), GroupInfor = tcga_df %>%
    select(sample, riskgroup) %>%
    rename(Score = 2) %>%
    mutate(Score = factor(Score, levels = c("Low", "High"))), Df_For_Corr = tcga_df %>%
    select(sample, riskscore) %>%
    rename(Score = 2), saveFile = T, output_dir = paste0(out_home, "/out/8.drug/"),
    DrugSelected = drug_name, y_lab_chr = NULL)

cor.data <- data.frame(drug = cor_res_df$drug, r = cor_res_df[, "rho"], p = -log10(cor_res_df[,
    "adjp"]))

cor.data <- cor.data %>%
    filter(drug %in% drug_name)

p1 <- ggplot(data = cor.data, aes(r, forcats::fct_reorder(drug, r, .desc = T))) +
    geom_segment(aes(xend = 0, yend = drug), linetype = 2) + geom_point(aes(size = p),
    col = RColorBrewer::brewer.pal(4, "Set1")[4]) + 
    expand = expansion(mult = c(0.01,.1)) + #左右留空 #左右留空
    ggpubr::theme_pubr() + labs(x = "Rho", y = "", size = bquote("-log10(adj.P)")) +
    theme(legend.position = "bottom", axis.ticks.y.right = element_blank(), plot.margin = unit(c(0.2,
        0.2, 0.2, 0), "cm"))

box_data <- IC50 %>%
    rownames_to_column("sample") %>%
    pivot_longer(cols = -sample, values_to = "logic50", names_to = "drug") %>%
    inner_join(tcga_df %>%
        select(sample, riskgroup))

p4 <- ggplot(box_data %>%
    filter(drug %in% drug_name), aes(drug, logic50, fill = riskgroup)) + geom_boxplot(aes(col = riskgroup),
    outlier.size = 0.6, width = 0.4) + scale_fill_manual(values = RColorBrewer::brewer.pal(4,
    "Set1")[1:2]) + scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set1")[1:2]) +
    xlab(NULL) + ylab("log(IC50)") + ggpubr::theme_pubr(13) + theme(axis.text.x = element_text(angle = 45,
    hjust = 1, vjust = 1, size = 10), legend.position = "bottom", legend.title = element_blank()) +
    ggpubr::stat_compare_means(aes(group = riskgroup, label = ..p.signif..)) + stat_summary(fun = mean,
    geom = "crossbar", color = "white", width = 0.41, size = 0.15, position = position_dodge(width = 0.4))

p5_pos <- cowplot::plot_grid(p1, p4, nrow = 1)
plotout(p = p5_pos, od = paste0(out_home, "/out/8.drug/"), name = "drug_pos", w = 7,
    h = 4.4)


# 负相关
drug_name <- cor_res_df %>%
    filter(rho < 0) %>%
    arrange(adjp) %>%
    head() %>%
    pull(1)

a <- Drug_IC50(DrugInfor = IC50 %>%
    rownames_to_column("sample"), GroupInfor = tcga_df %>%
    select(sample, riskgroup) %>%
    rename(Score = 2) %>%
    mutate(Score = factor(Score, levels = c("Low", "High"))), Df_For_Corr = tcga_df %>%
    select(sample, riskscore) %>%
    rename(Score = 2), saveFile = T, output_dir = paste0(out_home, "/out/8.drug/neg/"),
    DrugSelected = drug_name, y_lab_chr = NULL)

cor.data <- data.frame(drug = cor_res_df$drug, r = cor_res_df[, "rho"], p = -log10(cor_res_df[,
    "adjp"]))

cor.data <- cor.data %>%
    filter(drug %in% drug_name)

p1 <- ggplot(data = cor.data, aes(r, forcats::fct_reorder(drug, r, .desc = F))) +
    geom_segment(aes(xend = 0, yend = drug), linetype = 2) + geom_point(aes(size = p),
    col = RColorBrewer::brewer.pal(4, "Set1")[4]) + #   scale_size_continuous(range =c(2,8)) +    scale_x_reverse(breaks = c(0, -0.3, -0.5),                    expand = expansion(mult = c(0.01,.1))) + #左右留空 col
                      expand = expansion(mult = c(0.01,.1)) + #左右留空 #左右留空
ggpubr::theme_pubr() + labs(x = "Rho", y = "", size = bquote("-log10(adj.P)")) +
    theme(legend.position = "bottom", axis.ticks.y.right = element_blank(), plot.margin = unit(c(0.2,
        0.2, 0.2, 0), "cm")) + scale_x_continuous(expand = c(0, 0))

box_data <- IC50 %>%
    rownames_to_column("sample") %>%
    pivot_longer(cols = -sample, values_to = "logic50", names_to = "drug") %>%
    inner_join(tcga_df %>%
        select(sample, riskgroup))

p4 <- ggplot(box_data %>%
    filter(drug %in% drug_name), aes(drug, logic50, fill = riskgroup)) + geom_boxplot(aes(col = riskgroup),
    outlier.size = 0.6) + scale_fill_manual(values = RColorBrewer::brewer.pal(4,
    "Set1")[1:2]) + scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set1")[1:2]) +
    xlab(NULL) + ylab("log(IC50)") + ggpubr::theme_pubr(13) + theme(axis.text.x = element_text(angle = 45,
    hjust = 1, vjust = 1, size = 10), legend.position = "bottom", legend.title = element_blank()) +
    ggpubr::stat_compare_means(aes(group = riskgroup, label = ..p.signif..)) + stat_summary(fun = mean,
    geom = "crossbar", color = "white", width = 0.41, size = 0.15, position = position_dodge(width = 0.4))

p5_neg <- cowplot::plot_grid(p1, p4, nrow = 1)
plotout(p = p5_neg, od = paste0(out_home, "/out/8.drug/"), name = "drug_neg", w = 9,
    h = 5)

p5 <- cowplot::plot_grid(p5_pos, p5_neg, nrow = 2, labels = "AUTO")
plotout(p = p5, od = paste0(out_home, "/out/8.drug/"), name = "drug_pos_neg", w = 10,
    h = 9)


class(p5_pos)


seq_len(nrow(box_data))
